<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/12-16:33
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\wechat\controller;

    use app\wechat\service\MediaService;
    use think\Db;
    class News extends ChatBase
    {

        /**
         * 设置默认操作表
         * @var string
         */
        protected $table = 'WechatNews';

        /**
         * @throws \think\Exception
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: index
         * @describe:微信图文管理
         */
        public function index()
        {
            $this->title = '微信图文列表';
            $this->_query($this->table)->where(['is_deleted' => '0'])->order('id desc')->page();
        }



        /**
         * @param $data
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: _index_page_filter
         * @describe:图文列表数据处理
         */
        protected function _index_page_filter(&$data)
        {
            foreach ($data as &$vo) $vo = MediaService::news($vo['id']);
        }



        /**
         * @throws \think\Exception
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: select
         * @describe:图文选择器
         */
        public function select()
        {
           return $this->index();
        }



        /**
         * @param $data
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: _select_page_filter
         * @describe:图文列表数据处理
         */
        protected function _select_page_filter(&$data)
        {
            foreach ($data as &$vo) $vo = MediaService::news($vo['id']);
        }



        /**
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: add
         * @describe:添加微信图文
         */
        public function add()
        {
            if ($this->request->isGet()) {
                $this->title = '新建图文';
                return $this->fetch('form');
            } else {
                $data = $this->request->post();
                if (($ids = $this->_apply_news_article($data['data'])) && !empty($ids)) {
                    if (dataSave($this->table, ['article_id' => $ids, 'create_by' => session('user.id')], 'id') !== false) {
                        $url = url('@admin') . '#' . url('@wechat/news/index') . '?spm=' . $this->request->get('spm');
                        $this->success('图文添加成功！', $url);
                    }
                }
                $this->error('图文添加失败，请稍候再试！');
            }
        }



        /**
         * @throws \think\Exception
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: edit
         * @describe:编辑微信图文
         */
        public function edit()
        {
            if (($this->id = $this->request->get('id')) < 1) {
                $this->error('参数错误，请稍候再试！');
            }
            if ($this->request->isGet()) {
                if ($this->request->get('output') === 'json') {
                    $this->success('获取数据成功！', MediaService::news($this->id));
                } else {
                    $this->fetch('form', ['title' => '编辑图文']);
                }
            } else {
                $post = $this->request->post();
                if (isset($post['data']) && ($ids = $this->_apply_news_article($post['data']))) {
                    if (dataSave('wechat_news', ['id' => $this->id, 'article_id' => $ids], 'id')) {
                        $this->success('图文更新成功！', url('@admin') . '#' . url('@wechat/news/index'));
                    }
                }
                $this->error('图文更新失败，请稍候再试！');
            }
        }



        /**
         * @param $data
         * @param array $ids
         * @return string
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: _apply_news_article
         * @describe:图文更新操作
         */
        private function _apply_news_article($data, $ids = [])
        {
            foreach ($data as &$vo) {
                if (empty($vo['digest'])) {
                    $vo['digest'] = mb_substr(strip_tags(str_replace(["\s", '　'], '', $vo['content'])), 0, 120);
                }
                $vo['create_at'] = date('Y-m-d H:i:s');
                if (empty($vo['id'])) {
                    $result = $id = Db::name('WechatNewsArticle')->insertGetId($vo);
                } else {
                    $id = intval($vo['id']);
                    $result = Db::name('WechatNewsArticle')->where('id', $id)->update($vo);
                }
                if ($result !== false) {
                    array_push($ids, $id);
                }
            }
            return join(',', $ids);
        }



        /**
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: remove
         * @describe:删除微信图文
         */
        public function remove()
        {
            $this->_delete($this->table);
        }


    }